package com.htl.线程.sleep_wait;

import java.util.concurrent.TimeUnit;

public class Sleep_wait2 {
    public static void main(String[] args) {
        Data2 data2 = new Data2();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data2.test(i);
            }
        }).start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(8);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            data2.test2();
        }).start();
    }
}


class Data2{
    // 在i==5的时候让线程休眠
    public synchronized void test(int i){
        if (i == 5) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(i + "----------------------------");
    }

    // 唤醒线程
    public synchronized void test2(){
        this.notify();
    }

}
